package 栈和队列;

import java.util.Stack;

/**
 * ---------------------------------------------------------
 * <h>https://leetcode-cn.com/problems/min-stack/</h>
 * <p>
 * 设计一个支持 push ，pop ，top 操作，并能在常数时间内检索到最小元素的栈。
 *
 * push(x) —— 将元素 x 推入栈中。
 * pop() —— 删除栈顶的元素。
 * top() —— 获取栈顶元素。
 * getMin() —— 检索栈中的最小元素。
 *  
 *
 * 示例:
 *
 * 输入：
 * ["MinStack","push","push","push","getMin","pop","top","getMin"]
 * [[],[-2],[0],[-3],[],[],[],[]]
 *
 * 输出：
 * [null,null,null,null,-3,null,0,-2]
 *
 * 解释：
 * MinStack minStack = new MinStack();
 * minStack.push(-2);
 * minStack.push(0);
 * minStack.push(-3);
 * minStack.getMin();   --> 返回 -3.
 * minStack.pop();
 * minStack.top();      --> 返回 0.
 * minStack.getMin();   --> 返回 -2.
 *  
 *
 * 提示：
 *
 * pop、top 和 getMin 操作总是在 非空栈 上调用。
 *
 * </p>
 * Created by Frank on 2021/3/31.
 * <a href="mailto:frankyao10110@gmail.com">Contact me</a>
 * ---------------------------------------------------------
 */
public class _最小栈 {
    public static void main(String[] args) {
//        _最小栈_双栈 minStack = new _最小栈_双栈();
        _最小栈_链表 minStack = new _最小栈_链表();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println("min: " + minStack.getMin());
        minStack.pop();
        System.out.println("top: " + minStack.top());
        System.out.println("min: " + minStack.getMin());

    }
    //方法一：维护两个栈，一个正常栈（-3,0,-2)，一个放最小值的栈 (-3, -2, -2)
    public static class _最小栈_双栈 {
        private Stack<Integer> stack;
        private Stack<Integer> minStack;

        public _最小栈_双栈() {
            stack = new Stack<>();
            minStack = new Stack<>();
        }

        public void push(int value) {
            stack.push(value);
            if (minStack.isEmpty() || minStack.peek() > value) {
                minStack.push(value);
            }else{
                minStack.push(minStack.peek());
            }
        }

        public void pop() {
            stack.pop();
            if (!minStack.isEmpty()) {
                minStack.pop();
            }
        }

        public int top() {
            return stack.peek();
        }

        public int getMin() {
            return minStack.peek();
        }
    }

    //方法二：用链表维护一个最小栈，Node节点存放
    public static class _最小栈_链表 {

        private Node minStack;

        public _最小栈_链表() {
            minStack = new Node(Integer.MAX_VALUE, 0, null);
        }

        public void push(int val) {
            int min = Math.min(val, minStack.min);
            minStack = new Node(min, val, minStack);
        }

        public void pop() {
            minStack = minStack.next;
        }

        public int top() {
            return minStack.val;
        }

        public int getMin() {
            return minStack.min;
        }

        private static class Node {
            private final int min;
            private final int val;
            private Node next;

            public Node(int min, int val, Node next) {
                this.min = min;
                this.val = val;
                this.next = next;
            }
        }
    }
}
